package net.zhengxinyun.performance.App.service.EvalScoreMag;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.performance.App.entity.EvaluateIndexEnrity;
import net.zhengxinyun.performance.App.entity.EvaluateScoreEntity;
import net.zhengxinyun.performance.App.mapper.EvaScoreMageMapper;
import net.zhengxinyun.performance.App.service.UnionOtherSys.UnionOtherSysService;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.EvaluateIndexFilter;
import net.zhengxinyun.performance.filter.EvaluateScoreFilter;
import net.zhengxinyun.performance.mapper.criteria.CriteriaEntityMapper;
import net.zhengxinyun.performance.mapper.login.UserEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Service
public class EvalScoreMagServiceImpl  implements  EvalScoreMagService  {
    @Autowired
    private EvaScoreMageMapper mageMapper;
    @Autowired
    private CriteriaEntityMapper criteriaEntityMapper;
    @Autowired
    private UserEntityMapper userEntityMapper;

    @Autowired
    private UnionOtherSysService unionSysService;

    @Override
    public int addEvaMag(EvaluateIndexEnrity enrity) {
        if (StringUtils.isBlank(enrity.getConnects())) {
            enrity.setConnects(0+"");
        }
        return mageMapper.addEvaMag(enrity);
    }

    @Override
    public int delEvaMag(Long id) {
        int i = mageMapper.delEvaMag(id);
        if(i>0){
            criteriaEntityMapper.delByEvalId(id);
        }
        return i;
    }

    @Override
    public int updEvaMag(EvaluateIndexEnrity enrity) {
        if (StringUtils.isBlank(enrity.getConnects())) {
            enrity.setConnects(0+"");
        }
        return mageMapper.updEvaMag(enrity);
    }

    // 所有考核分类
    @Override
    public List<EvaluateIndexFilter> queryEvaMag(EvaluateIndexFilter filter) {

        List<EvaluateIndexFilter> evaluateIndexEnrities = mageMapper.queryEvaMag(filter);
        return  evaluateIndexEnrities;

    }

    // 考核事项详情
    @Override
    public List<EvaluateScoreEntity> queryEvainfo(Long evaId,String name) {
        return mageMapper.queryEvainfo(evaId, name);
    }

    @Override
    public int addEvaScore(EvaluateScoreEntity enrity) {

        return mageMapper.addEvaScore(enrity);
    }

    @Override
    public int saveOrUpdateEvaScore(EvaluateScoreEntity entity) {
        EvaluateScoreFilter filter = new EvaluateScoreFilter();
        filter.setSysCode(entity.getSysCode());
        filter.setLeadName(entity.getLeadName());
        filter.setEvaId(entity.getEvaId());
        List<EvaluateScoreFilter> entitys = queryEvaScore(filter);
        int i =0;

        if(ObjectUtils.isBlank(entitys)){
            i =mageMapper.addEvaScore(entity);
        }else {
            i = mageMapper.updateScoreByPrimaryKeySelective(entity);
        }

        return i;
    }

    @Override
    public int delEvaScore(Long id) {
        return mageMapper.delEvaScore(id);
    }

    @Override
    public int updEvaScore(EvaluateScoreEntity enrity) {
        return mageMapper.updEvaScore(enrity);
    }


    @Override
    public int updateScoreByPrimaryKeySelective(EvaluateScoreEntity entity) {
        return mageMapper.updateScoreByPrimaryKeySelective(entity);
    }

    @Override
    public List<EvaluateScoreFilter> queryEvaScore(EvaluateScoreFilter filter) {


        return  mageMapper.queryEvaScore(filter);
    }

    @Override
    public int batchSaveEvaScore(List<EvaluateScoreEntity> lists) {
        int i =0;
        if(null==lists){
            return i;
        }
        for (EvaluateScoreEntity list : lists) {
            //判断是否存在数据；
            EvaluateScoreFilter filter = new EvaluateScoreFilter();
            filter.setSysCode(list.getSysCode());
            filter.setEvaId(list.getEvaId());
            filter.setItemid(list.getItemid());
            filter.setLeadName(list.getLeadName());
            List<EvaluateScoreFilter> flags = mageMapper.queryEvaScore(filter);
            if(ObjectUtils.isBlank(flags)){
                int t =addEvaScore(list);
                i=i+t;
            }


        }
        return i;
    }

    /**
     * 其他系统获取护具
     * @param connects
     * @param leadId
     * @param criteriaType
     * @return
     * @throws IOException
     */
    @Override
    public   List<EvaluateScoreEntity> getDataUnionSys(String connects, Long leadId, String criteriaType) throws IOException {

        UserEntity userEntity = userEntityMapper.selectByPrimaryKey(leadId);
        String userid=null;
        if(ObjectUtils.isBlank(userEntity))
        {
            return null;
        }
        /*
        criteriaType  累计类 和 非累计类  累计类按数目计算，非累计类按照具体事项计算；
         */

        List<EvaluateScoreEntity> list=null;
        switch (connects){

            case "ZJXM":
                list= unionSysService.ZD_ZaiJianProject(userEntity.getUserPhone(), null);
                if(null==list){
                    return  null;
                }

                break;
            case "DCZDWORK":
                HashMap<String, Object> duchaMap = unionSysService.DC_userInfo(userEntity.getUserPhone());
                userid=duchaMap.get("userId")+ "";
                list = unionSysService.DC_ZhongDianWork(userid,criteriaType );
                if(null==list){
                    return  null;
                }
                break;

            case "DCPSJ":

                HashMap<String, Object> userinfomap = unionSysService.DC_userInfo(userEntity.getUserPhone());
                userid =userinfomap.get("userId")+"";
                list = unionSysService.DC_PishiJian(userid,criteriaType );
                if(null==list){
                    return  null;
                }

                break;
            default:
                return  null;

        }
        list.forEach(entity -> entity.setLeadName(leadId+""));
        return list;
    }
}
